#ifndef SERVER_H_
#define SERVER_H_

#include <stdint.h>

#include <string>

#include "socket.h"

class Server {
 private:

  std::string hostname_;
  uint16_t port_;

  /* Copy and Assignment are Disallowed. */
  Server(const Server& node);
  Server& operator=(const Server& rhs);

  /**
   * @brief Initializes the class by setting up the listening socket
   */
  void Init();

 protected:

  /**
   * @brief Socket that is used to listen for incoming TCP
   * connections.
   */
  Socket socket;

 public:

  /**
   * @brief Constructs a new node with the given hostname and port
   *
   * @param hostname
   * @param port
   */
  Server(const char* hostname, uint16_t port);

  /**
   * @brief Closes the socket
   */
  virtual ~Server();

  /**
   * @return the hostname of the name.
   */
  inline const std::string& hostname() const;

  /**
   * @return the port number used to accept connections on this node.
   */
  inline uint16_t port() const;

  /**
   * @brief Blocks listening for new connections from other heartbeat nodes. 
   */
  void AcceptConnections();

  /**
   * @brief Callback used when a new request comes in.  All subclasses
   * need to implement this method.
   */
  virtual void HandleRequest(Socket* socket) = 0;
};

inline const std::string & Server::hostname() const {
  return this->hostname_;
}

inline uint16_t Server::port() const {
  return this->port_;
}

#endif  // SERVER_H_
